package com.cheyun.netsalev3.repository

import androidx.paging.PageKeyedDataSource
import com.cheyun.netsalev3.bean.AllocatedListInfo
import com.cheyun.netsalev3.bean.AllocatedParam
import com.cheyun.netsalev3.utils.api.*
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers


class AllocatedListPageRepository(var state: String,var map: Map<String, String> = HashMap()) :BasePageRepository<AllocatedListInfo>() {

    fun changeMap(newMap:HashMap<String, String>){
        this.map = newMap
    }

    override fun loadInitial(params: PageKeyedDataSource.LoadInitialParams<Int>, callback: (data: MutableList<AllocatedListInfo>) -> Unit) {
        ApiClient.getInstance().getAllocated(1, state, map)
                .subscribeOn(Schedulers.io())
                .subscribeOn(AndroidSchedulers.mainThread())
                .subscribe(object : ApiCallback<BaseResp<AllocatedParam>>() {
                    override fun onSuccess(t: BaseResp<AllocatedParam>) {
                        t.data?.rows?.apply {
                            total.postValue(t.data!!.total)
                            operXphone(this)
                            callback(this)
                        }
                    }

                    override fun onError(ex: ApiException) {
                        networkState.postValue(NetworkState.error(ex.getMessage()))
                    }


                })
    }

    override fun loadAfter(params: PageKeyedDataSource.LoadParams<Int>, callback: PageKeyedDataSource.LoadCallback<Int, AllocatedListInfo>) {
        ApiClient.getInstance().getAllocated(params.key, state, map)
                .subscribeOn(Schedulers.io())
                .subscribeOn(AndroidSchedulers.mainThread())
                .subscribe(object : ApiCallback<BaseResp<AllocatedParam>>() {
                    override fun onSuccess(t: BaseResp<AllocatedParam>) {
                        t.data?.rows?.apply {
                            operXphone(this)
                            callback.onResult(this, params.key + 1)
                        }
                    }

                    override fun onError(ex: ApiException) {

                    }
                })
    }

    override fun loadBefore(params: PageKeyedDataSource.LoadParams<Int>, callback: PageKeyedDataSource.LoadCallback<Int, AllocatedListInfo>) {
        ApiClient.getInstance().getAllocated(params.key,state, map)
                .subscribeOn(Schedulers.io())
                .subscribeOn(AndroidSchedulers.mainThread())
                .subscribe(object : ApiCallback<BaseResp<AllocatedParam>>() {
                    override fun onSuccess(t: BaseResp<AllocatedParam>){
                        t.data?.rows?.apply {
                            operXphone(this)
                            callback.onResult(this, params.key - 1)
                        }
                    }

                    override fun onError(ex: ApiException) {

                    }
                })
    }


    fun operXphone(data: MutableList<AllocatedListInfo>){
        data.forEach {
            if(it.xphone.isNullOrEmpty()){
                it.xphone = it.phone
            }
        }
    }



}